<?php
/**
 * Url 处理
 * @author  ZhouHaoHua <53027327@qq.com>
 */
class Url extends \core\models\Url {

    /**
     * 最终的URL地址
     * @var string
     */
    protected $page = '';

    /**
     * 传进来处理过后的参数数组
     * @var array
     */
    protected $params = array();

    /**
     * 需要转换的参数,生成URL时把val转化成key,还原时找key还原成值
     * @var array
     */
    protected $transform_parameters = array();

    /**
     * 参数分隔符
     * @var string
     */
    protected $separator = '/';

    /**
     * 默认的URL扩展名
     * @var string
     */
    protected $extension = '.html';
    private function addRESTfulAction($url){
        $method = $_SERVER['REQUEST_METHOD'] ? strtolower($_SERVER['REQUEST_METHOD']) : 'get';
        if ($url == '/') $url = '/index';
        $tmp = explode('/',$url);
        $tmp[1] = $tmp[1] . '/' . $method;
        return implode('/',$tmp);
    }
    /**
     * 分析并还原URL为对应的原变量
     * @param string $url_str 这个值的格式与$_SERVER['REQUEST_URI']相同，若不传此值就默认取$_SERVER['REQUEST_URI']的值
     * @return bool 如果返回true就代表有404错误
     */
    public function parseUrl($url_str = null) {
        if(in_array(\config_web::API_OUT_TYPE, ['json','xml'])) {
            $url_str = !!$url_str ? $url_str : $_SERVER['REQUEST_URI'];
            if( stripos( $url_str, '?' ) !== false ) {
                $tmp = explode( '?', $url_str );
                $tmp[0] = $this->addRESTfulAction( $tmp[0] );
                if( substr( $tmp[0], -1 ) !== '/' ) {
                    $tmp[0] = $tmp[0] . '/';
                }
                $url_str = implode( '?', $tmp );
            } else {
                $url_str = $this->addRESTfulAction( $url_str );
            }
        }
        $rtn = parent::parseUrl($url_str);
        return $rtn;
    }
}